package com.shelpe.services.simbasync.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.shelpe.services.simbasync.dto.SyncRecordDto;
import com.shelpe.services.simbasync.model.mongo.SyncPerDayRecord;
import com.shelpe.services.simbasync.service.AdgroupsService;
import com.shelpe.services.simbasync.service.CampaignsService;
import com.shelpe.services.simbasync.service.SyncRecordService;
import com.shelpe.services.simbasync.service.UserService;

@RestController
@RequestMapping(value="/simbasync")
public class SyncRecordController {

	private static final Logger logger = LoggerFactory.getLogger(SyncRecordController.class);
	
	@Autowired
	private SyncRecordService syncService;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private CampaignsService campaignService;
	
	@Autowired
	private AdgroupsService adgroupService;
	
	@RequestMapping("/deletesyncrecord")
	@ResponseBody
	public SyncRecordDto deleteAllSyncRecord(){
		
		logger.info(">>>deleteAllSyncRecord begin");
		SyncRecordDto dto = new SyncRecordDto();
		dto.setProcess("deleteAllSyncRecord");
		Date beginProcessTime = new Date();
		
		syncService.deleteAllSyncRecord();
		
		Date endProcessTime = new Date();
		long durTime = endProcessTime.getTime() - beginProcessTime.getTime();
		dto.setDurTime(durTime);
		logger.info(">>>deleteAllSyncRecord end");
		
		return dto;
	}
	
	@RequestMapping("/needsyncagain")
	@ResponseBody
	public SyncRecordDto needSyncAgain(){
		
		SyncRecordDto dto = new SyncRecordDto();
		dto.setProcess("needSyncAgain");
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String dateNowStr = sdf.format(date);
		Date dateNowFormat = new Date();
		try {
			dateNowFormat = sdf.parse(dateNowStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Calendar calendar = Calendar.getInstance(); 
		calendar.setTime(dateNowFormat); 
		calendar.add(Calendar.HOUR,-24);//减一天
		int countLastOneDay = userService.countRptCustomerByDate(calendar.getTime());
		calendar.add(Calendar.HOUR,-24);//减一天
		int countLastTwoDay = userService.countRptCustomerByDate(calendar.getTime());
		boolean needSync = (countLastOneDay + 1.0) / (countLastTwoDay + 1.0) < 0.4;
		logger.info(">>>>>>>>>>>>needSyncAgain lastOne: {}, lastTwo: {}, TwoDay: {}, needSync: {}", countLastOneDay, countLastTwoDay, calendar.getTime(), needSync);
		needSync = needSync || needSync();
		dto.setNeedSyncAgain(needSync);

		return dto;
	}
	
	private boolean needSync(){
		
		boolean needSync = false;
		int userNum = userService.countAll();
		int campaignNum = campaignService.countAll();
		int adgroupNum = adgroupService.countAll();
		logger.info(">>>>>>>>>>>>needSyncAgain userNum = {}, campaignNum = {}, adgroupNum = {}", userNum, campaignNum, adgroupNum);
		int userFailed = 0;
		SyncPerDayRecord record = syncService.getSyncPerDayRecordById("syncRptCustomer");
		if(record != null){
			userFailed = record.getFailedNum() == null? 0 : record.getFailedNum();
			needSync = needSync || (userFailed / (userNum + 0.1) > 0.01);
		}
		logger.info(">>>>>>>>>>>>needSyncAgain userFailed = {}", userFailed);
		int campaignFailed = 0;
		record = syncService.getSyncPerDayRecordById("syncRptCampaign");
		if(record != null){
			campaignFailed = record.getFailedNum() == null? 0 : record.getFailedNum();
			needSync = needSync || (campaignFailed / (campaignNum + 0.1) > 0.01);
		}
		logger.info(">>>>>>>>>>>>needSyncAgain campaignFailed = {}", campaignFailed);
		int adgroupFailed = 0;
		record = syncService.getSyncPerDayRecordById("syncRptAdgroup");
		if(record != null){
			adgroupFailed = record.getFailedNum() == null? 0 : record.getFailedNum();
			needSync = needSync || (adgroupFailed / (adgroupNum + 0.1) > 0.01);
		}
		logger.info(">>>>>>>>>>>>needSyncAgain adgroupFailed = {}", adgroupFailed);
		int creativeFailed = 0;
		record = syncService.getSyncPerDayRecordById("syncRptCreative");
		if(record != null){
			creativeFailed = record.getFailedNum() == null? 0 : record.getFailedNum();
			needSync = needSync || (creativeFailed / (adgroupNum + 0.1) > 0.01);
		}
		logger.info(">>>>>>>>>>>>needSyncAgain creativeFailed = {}", creativeFailed);
		int keywordFailed = 0;
		record = syncService.getSyncPerDayRecordById("syncRptKeyword");
		if(record != null){
			keywordFailed = record.getFailedNum() == null? 0 : record.getFailedNum();
			needSync = needSync || (keywordFailed / (adgroupNum + 0.1) > 0.01);
		}
		logger.info(">>>>>>>>>>>>needSyncAgain keywordFailed = {}, needSync = {}", keywordFailed, needSync);
		
		return needSync;
	}
}
